package edu.hawaii.myisern.action;

import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebForm;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;
import static org.junit.Assert.assertEquals;
import org.junit.Before;
import org.junit.Test;


/**
 * Tests login operation of the Login Action Bean.
 *
 * @author Lisa Chen  
 */
public class TestLoginActionBean extends TestWeb {
  
  /** constants used in testing to appease PMD */
  private static final String PASSWORD_FORM_FIELD = "password";
  private static final String USERNAME_FORM_FIELD = "username";
  private static final String LOGIN_PAGE_TITLE = "My ISERN Login";
  private static final String PASSWORD = "isern2008";
  private static final String USERNAME = "myisern";

  /**
   * Override the create session.
   */
  @Override
  @Before
  public void createSession() { 
    this.conversation = new WebConversation();
  }
  
  /**
   * Test security filter on non public pages when not logged in.
   * 
   * @throws Exception If problems occur
   */
  @Test
  public void testSecurityFilter() throws Exception {
    // Try to access a non public page in MyIsern when not logged in.
    String Url = getAppUrl() + "/researcher_form.jsp";
    WebResponse response = conversation.getResponse(Url);
    assertEquals("Check access to non public page is redirected", 
        LOGIN_PAGE_TITLE, response.getTitle());
  }
  
  /**
   * Tests Isern login form with empty parameters.
   *
   * @throws Exception If problems occur
   */
  @Test
  public void testEmptyLogin() throws Exception {
    // Get login.jsp ensure the title matches
    String Url = getAppUrl() + "/login.jsp";
    WebResponse response = conversation.getResponse(Url);
    assertEquals("Retrieving login page.", LOGIN_PAGE_TITLE, response.getTitle());

    // Submit empty login form
    WebForm loginForm = response.getFormWithID("LoginForm");
    WebRequest loginRequest = loginForm.getRequest();
    response = conversation.getResponse(loginRequest);
    assertEquals("Check empty login err message (username).", true, 
        response.getText().contains("Username is a required field"));
    assertEquals("Check empty login err message (password).", true, 
        response.getText().contains("Password is a required field"));
        
    // Submit just empty password
    loginRequest.setParameter(USERNAME_FORM_FIELD, USERNAME);
    loginRequest.setParameter(PASSWORD_FORM_FIELD, "");    
    response = conversation.getResponse(loginRequest);
    assertEquals("Check empty login error message (password).", true, 
        response.getText().contains("Password is a required field"));

    // Submit just empty username
    loginRequest.setParameter(USERNAME_FORM_FIELD, "");
    loginRequest.setParameter(PASSWORD_FORM_FIELD, "kevin");
    response = conversation.getResponse(loginRequest);
    assertEquals("Check empty login error message(username).", true, 
        response.getText().contains("Username is a required field"));
  }
  
  /**
   * Tests Isern login form with invalid parameters.
   *
   * @throws Exception If problems occur
   */
  @Test
  public void testInvalidLogin() throws Exception {
    this.conversation = new WebConversation();
    // Get login.jsp ensure the title matches
    String Url = testHost + applicationName + "/login.jsp";
    WebResponse response = conversation.getResponse(Url);
    assertEquals("Retrieving login page.", LOGIN_PAGE_TITLE, response.getTitle());
    
    WebForm loginForm = response.getFormWithID("LoginForm");
    WebRequest loginRequest = loginForm.getRequest();
    
    // Submit invalid username and invalid password
    loginRequest.setParameter(USERNAME_FORM_FIELD, "invalidUser");
    loginRequest.setParameter(PASSWORD_FORM_FIELD, "invalidPass");
    response = conversation.getResponse(loginRequest);
    assertEquals("Check empty login error message (username).", true, 
        response.getText().contains("Invalid User Name"));
    assertEquals("Check empty login error message (password).", true, 
        response.getText().contains("Invalid Password"));

    // Submit invalid username with valid password
    loginRequest.setParameter(USERNAME_FORM_FIELD, "myisern");
    loginRequest.setParameter(PASSWORD_FORM_FIELD, "invalidPass");
    response = conversation.getResponse(loginRequest);
    assertEquals("Check empty login error message (username).", true, 
        response.getText().contains("Invalid Password"));
    
    // Submit valid username with invalid password
    loginRequest.setParameter(USERNAME_FORM_FIELD, "invalidUser");
    loginRequest.setParameter(PASSWORD_FORM_FIELD, "isern2008");
    response = conversation.getResponse(loginRequest);
    assertEquals("Check empty login error message (username).", true, 
        response.getText().contains("Invalid User Name"));
  }
  
  /**
   * Tests Isern login form with valid parameters.
   *
   * @throws Exception If problems occur
   */
  @Test
  public void testValidLogin() throws Exception {
    this.conversation = new WebConversation();
    // Get login.jsp ensure the title matches
    String Url = testHost + applicationName + "/login.jsp";
    WebResponse response = conversation.getResponse(Url);
    assertEquals("Retrieving login page.", LOGIN_PAGE_TITLE, response.getTitle());
    
    WebForm loginForm = response.getFormWithID("LoginForm");
    WebRequest loginRequest = loginForm.getRequest();
    
    // Submit valid username and password
    loginRequest.setParameter(USERNAME_FORM_FIELD, USERNAME);
    loginRequest.setParameter(PASSWORD_FORM_FIELD, PASSWORD);
    response = conversation.getResponse(loginRequest);
    assertEquals("Check login forwarded to index.jsp", 
        "ISERN - Organizations", response.getTitle());
  }
}
  
  
